package com.java.mybatis.controller;

import com.java.mybatis.entity.Logistics;
import com.java.mybatis.service.LogisticsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Logistics)表控制层
 *
 * @author makejava
 * @since 2021-01-15 17:10:28
 */
@Controller
@RequestMapping("/logistics/")
public class LogisticsController {
    /**
     * 服务对象
     */
    @Resource
    private LogisticsService logisticsService;


    @RequestMapping("LogisticsLikeas")
    public String LogisticsLikeas(String logisticsNumber,Model model){
        System.out.println("测试数据");
        System.out.println(logisticsNumber);
        List<Logistics> logistics = this.logisticsService.LogisticsLikeas(logisticsNumber);
        System.out.println(logistics);
        if (logistics.size()>0){
            int size = logistics.size();
            model.addAttribute("size",size);
            //通过model把集合中的数据返回给运输信息管理页面
            model.addAttribute("logis",logistics);
            //把数据返回运输信息管理页面
            return "System/role-add";
        }
        return  "集合没有数据";
    }
    /**
     * 牟恩跃----取派时间管理模糊查询
     * @param logisticsNumber
     * @return
     */
    @RequestMapping("LogisticsLike")
    public String LogisticsLike(String logisticsNumber,Model model){
        //获取页面的数据，进行数据查询，存入集合
        List<Logistics> logistics = this.logisticsService.LogisticsLike("%" + logisticsNumber + "%");
        if (logistics.size()>0){
            //获取集合的个数
            int size = logistics.size();
            //通过Model返回数据给页面进行显示
            model.addAttribute("siz",size);
            //通过Model返回数据给页面进行显示
            model.addAttribute("logisti",logistics);
            //返回数据给页面
            return "System/sys-set";
        }
        return "集合没有数据";
    }
    /**
     * 牟恩跃--条件查询物流表数据
     * @param logistics
     * @return
     */
    @RequestMapping("LogisticsAllas")
    public String LogisticsAllas(Logistics logistics,Model model){
        //获取数据并存储到集合
        List<Logistics> logistics1 = this.logisticsService.LogisticsAllas(logistics);
        //获取集合的个数
        int size = logistics1.size();
        model.addAttribute("size",size);
        //通过model把集合中的数据返回给运输信息管理页面
        model.addAttribute("logis",logistics1);
        //把数据返回运输信息管理页面
        return "System/role-add";
    }

    /**
     * 牟恩跃--查询物流信息配送信息的所有数据
     * @param page  开始页码
     * @param model
     * @return
     */
    @RequestMapping("LogisticsAll")
    public String LogisticsAll( Model model,Integer page){
        List<Logistics> logistics = logisticsService.LogisticsAll((page - 1) * 7, 7);
//        //获取到所有数据后存入list集合
//        List<Logistics> logistics1 = this.logisticsService.LogisticsAll(logistics);
//        //获取集合的个数
//        int size = logistics1.size();
//        //通过Model返回数据给页面进行显示
//        model.addAttribute("siz",size);
//        //通过Model返回数据给页面进行显示
        model.addAttribute("logisti",logistics);
        model.addAttribute("thisPage",page);
        //返回数据给页面
        return "System/sys-set";
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Logistics selectOne(Integer id) {
        return this.logisticsService.queryById(id);
    }

    /**
     * 查询所有物流信息
     * @return
     */
    @RequestMapping("queryLogisticsAllPage")
    public String queryLogisticsAllPage(Model model, HttpSession session, Integer page){
        session.removeAttribute("logisticsNumber");
        List<Logistics> logistics = logisticsService.queryByLogistics((page-1)*7,7);
        System.out.println("----------------------"+logistics.size());
        //分页查询的货物信息
        model.addAttribute("logistics",logistics);
        //分页当前页
        session.setAttribute("thisPage",page);
        //根据货物状态货物总条数
        int count = logisticsService.queryLogisticsCount();
        //总共有多少页
        int logisticsPageMax = count % 7 == 0 ? count / 7 : count / 7 + 1;
        model.addAttribute("logisticsText","当前第"+page+"页,共"+logisticsPageMax+"页");
        //存入总共有多少页
        model.addAttribute("logisticsPageMax",logisticsPageMax);
        //存入总共多少条数据
        model.addAttribute("count",count);
        return "System/role-add";
    }

    @RequestMapping("queryLogisticsLikeLimit")
    public String queryLogisticsLikeLimit(Model model,HttpSession session, String logisticsNumber, Integer page){
        if (logisticsNumber!=null&logisticsNumber!="") {
            session.setAttribute("logisticsNumber",logisticsNumber);
            System.out.println("----------------===" + logisticsNumber);
            HashMap<String, Object> map = new HashMap<>();
            map.put("str","%"+logisticsNumber+"%");
            map.put("page",(page-1)*7);
            map.put("pageSize",7);
            List<Logistics> logistics = logisticsService.queryLogisticsLikeLimit(map);
            for (Logistics logistic : logistics) {
                System.out.println("--------------" + logistic);
            }
            //分页查询的货物信息
            model.addAttribute("logistics",logistics);
            //分页当前页
            session.setAttribute("thisPage",page);
            //根据货物状态货物总条数
            int count = logisticsService.queryLogisticsLikeCount(map);
            //总共有多少页
            int logisticsPageMax = count % 7 == 0 ? count / 7 : count / 7 + 1;
            model.addAttribute("logisticsText","当前第"+page+"页,共"+logisticsPageMax+"页");
            //存入总共有多少页
            model.addAttribute("logisticsPageMax",logisticsPageMax);
            //存入总共多少条数据
            model.addAttribute("count",count);
            return "System/role-add";
        }else{
            return "redirect:/logistics/queryLogisticsAllPage?page=1";
        }
    }

}